#include <stdio.h>

#define N 10
/**
 * File: Exercise1016.c
 * -------------------------------------------------------
 * 1.16 Show how to modify Program 1.3 to implement full path compression, 
 * where we complete each union operation by making every node that we touch point to the root of the new tree.
 * -------------------------------------------------------
 * 编译
 * gcc -o PathCompressionByFull PathCompressionByFull.c
 * -----------------------------------------------------
 * 运行
 * ./PathCompressionByFull
 * 3 4 4 9 8 0 2 3 5 6 2 9 5 9 7 3 4 8 5 6 0 2 6 1
 *
 */
int main(int argc, char **argv){
    int i, p, q, j, root;
    int id[N];
    int sz[N];
    for (i = 0; i < N; i++) {
        id[i] = i;
        sz[i] = 1;
    }
    while (scanf("%d %d\n", &p, &q) == 2) {
        for (i = p; i != id[i]; i = id[i]) {
            ;
        }
        root = i;
        for (i = p; i != id[i]; i = id[i]) {
            id[i] = root;
        }
        for (j = q; j != id[j]; j = id[j]) {
            ;
        }
        root = j;
        for (j = q; j != id[j]; j = id[j]) {
            id[j] = root;
        }
        if (i == j) {
            continue;
        }
        if (sz[i] < sz[j]) {
            id[i] = j;
            sz[j] += sz[i];
        }else {
            id[j] = i;
            sz[i] += sz[j];
        }
        printf("%d %d\n", p, q);
    }
    return 0;
}